//
//  UIViewController+Agent.m
//  IOS深入传值
//
//  Created by 江湖 on 2020/6/10.
//  Copyright © 2020 江湖. All rights reserved.
//

#import "UIViewController+Agent.h"
#import <objc/runtime.h>
@implementation UIViewController (Agent)

//获取当前控制器
+ (UIViewController *)getCurrentVC {
//    return [DDFBZDYWINDOW shareZDYWindow].rootViewController;
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    if (!window) {
        return nil;
    }
    UIView *tempView;
    for (UIView *subview in window.subviews) {
        if ([[subview.classForCoder description] isEqualToString:@"UILayoutContainerView"]) {
            tempView = subview;
            break;
        }
    }
    if (!tempView) {
        tempView = [window.subviews lastObject];
    }

    id nextResponder = [tempView nextResponder];

    while (![nextResponder isKindOfClass:[UIViewController class]] || [nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UITabBarController class]]) {
        tempView =  [tempView.subviews firstObject];

        if (!tempView) {
            return nil;
        }
        nextResponder = [tempView nextResponder];
    }
    return  (UIViewController *)nextResponder;
}

- (void)setDelegateVC:(UIViewController *)delegateVC{
    objc_setAssociatedObject(self, "delegateVC", delegateVC, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}
- (UIViewController *)delegateVC{
    return objc_getAssociatedObject(self, "delegateVC");
}

- (void)setViewBlock:(ViewBlock)viewBlock{
    objc_setAssociatedObject(self, "viewBlock", viewBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);

}
- (ViewBlock)viewBlock{
    return objc_getAssociatedObject(self, "viewBlock");
}
@end
